//
//  TimeViewController.m
//  shenghuochaxunheji
//
//  Created by Yangfan on 14/11/7.
//  Copyright (c) 2014年 4gread. All rights reserved.
//

#import "TimeViewController.h"

@interface TimeViewController ()

@end

@implementation TimeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    date = [NSDate date];
    calendar = [NSCalendar currentCalendar];
}

- (NSDictionary *)getYearMonthDayHourMinuteSecondFromCurrent {
    if (date == nil) {
        date = [NSDate date];
    }
    if (calendar == nil) {
        calendar = [NSCalendar currentCalendar];
    }
    comps =[calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit |NSDayCalendarUnit) fromDate:date];
    
    NSInteger year = [comps year];
    
    NSInteger month = [comps month];
    
    NSInteger day = [comps day];
    
    comps =[calendar components:(NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit)
            
                       fromDate:date];
    
    NSInteger hour = [comps hour];
    
    NSInteger minute = [comps minute];
    
    NSInteger second = [comps second];
    
    comps =[calendar components:(NSWeekCalendarUnit | NSWeekdayCalendarUnit |NSWeekdayOrdinalCalendarUnit)
            
                       fromDate:date];
    
    NSInteger weekday = [comps weekday]; // 星期几（注意，周日是“1”，周一是“2”。。。。）
    
    NSString *str = @"";
    switch (weekday) {
        case 1:
        str = @"星期日";
        break;
        case 2:
        str = @"星期一";
        break;
        case 3:
        str = @"星期二";
        break;
        case 4:
        str = @"星期三";
        break;
        case 5:
        str = @"星期四";
        break;
        case 6:
        str = @"星期五";
        break;
        case 7:
        str = @"星期六";
        break;
        
        default:
        break;
    }
    
    NSInteger weekdayOrdinal = [comps weekdayOrdinal]; // 这个月的第几周
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[self returnNumber:year], @"year", [self returnNumber:month], @"month", [self returnNumber:day], @"day", [self returnNumber:hour], @"hour", [self returnNumber:minute], @"minute", [self returnNumber:second], @"second", str, @"weekday", [self returnNumber:weekday], @"weekdayNumber", [self returnNumber:weekdayOrdinal], @"weekdayOrdinal", nil];
    
    return dic;
}

- (NSNumber *) returnNumber:(NSInteger)sender {
    return [NSNumber numberWithInteger:sender];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
